package com.nondo.dean.platform.service;

import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.nondo.dean.common.tool.StringUtils;
import com.nondo.dean.platform.domain.Coupon;
import com.nondo.dean.platform.domain.CouponExample;
import com.nondo.dean.platform.mapper.CouponMapper;
import com.nondo.dean.platform.tool.CouponUtils;

/**
 * 优惠券管理业务类
 * 
 * @author zhaoljin
 * @date 2018年5月7日
 */
@Component
public class CouponService {

    @Autowired
    private CouponMapper couponMapper;

    /**
     * 插入优惠券
     * @param coupon
     * @return
     */
    @Transactional
    public int addCoupon(Coupon coupon) {
        return couponMapper.insertSelective(coupon);
    }

    /**
     * 修改优惠券
     * @param coupon
     * @return
     */
    @Transactional
    public int updateCoupon(Coupon coupon) {
        return couponMapper.updateByPrimaryKeySelective(coupon);
    }

    /**
     * 按照主键ID查询优惠券
     * @param id
     * @return
     */
    public Coupon selectCoupon(Integer id) {
        return couponMapper.selectByPrimaryKey(id);
    }

    /**
     * 优惠券分页查询列表
     * @param offset
     * @param limit
     * @param coupon
     * @return
     */
    public List<Coupon> getCouponList(int offset, int limit, Coupon coupon) {
        CouponExample example = new CouponExample();
        example.setOrderByClause("create_date DESC");
        RowBounds rowBounds = new RowBounds(offset, limit);
        CouponExample.Criteria criteria = example.createCriteria();
        setCriteria(criteria, coupon);
        return couponMapper.selectByExampleWithRowbounds(example, rowBounds);
    }

    /**
     * 优惠券总数统计
     * @param coupon
     * @return
     */
    public Long count(Coupon coupon) {
        CouponExample example = new CouponExample();
        CouponExample.Criteria criteria = example.createCriteria();
        setCriteria(criteria, coupon);
        return couponMapper.countByExample(example);
    }

    /**
     * 组装查询条件
     * @param criteria
     * @param coupon
     */
    void setCriteria(CouponExample.Criteria criteria, Coupon coupon) {
        if (coupon != null) {
            if (coupon.getId() != null)
                criteria.andIdEqualTo(coupon.getId());
            if (StringUtils.isNoneBlank(coupon.getCode()))
                criteria.andCodeLike("%" + coupon.getCode() + "%");
        }
        criteria.andDelFlagEqualTo(Coupon.DEL_FLAG_NORMAL);
    }

   /**
    * 按照优惠券名称查询优惠券
    * @param name
    * @return
    */
    public Coupon selectCouponByCode(String code) {
        CouponExample example = new CouponExample();
        CouponExample.Criteria criteria = example.createCriteria();
        criteria.andCodeEqualTo(code);
        criteria.andDelFlagEqualTo(Coupon.DEL_FLAG_NORMAL);
        List<Coupon> couponList = couponMapper.selectByExample(example);
        return CollectionUtils.isNotEmpty(couponList)?couponList.get(0):null;
    }
    
    /**
     * 生成客户优惠券
     * @param customerId 客户ID
     */
    public void generateCouponList(Integer customerId) {
        List<Coupon> couponList = CouponUtils.getCouponList();
        for (Coupon coupon : couponList) {
            Coupon couponDb = couponMapper.selectByPrimaryKey(customerId);
            if (couponDb==null) {
                coupon.setCustomerId(customerId);
                couponMapper.insertSelective(coupon);  
            }
        }
    }

    /**
     * 获取优惠券列表
     * 
     * @param customerId
     * @return List<{@link Coupon}> 优惠券列表
     */
    public List<Coupon> getCouponList(Integer customerId) {
        CouponExample example = new CouponExample();
        CouponExample.Criteria criteria = example.createCriteria();
        criteria.andCustomerIdEqualTo(customerId);
        criteria.andDelFlagEqualTo(Coupon.DEL_FLAG_NORMAL);
        List<Coupon> couponList = couponMapper.selectByExample(example);
        return couponList;
    }
    
}
